package org.jiuling.aliyun.sms.factory.service.impl;

import com.aliyun.dysmsapi20170525.models.*;
import org.jiuling.aliyun.sms.config.AliYunSmsConfig;
import org.jiuling.aliyun.sms.convert.*;
import org.jiuling.aliyun.sms.dto.SignFileListDto;
import org.jiuling.aliyun.sms.enums.SignSourceEnum;
import org.jiuling.aliyun.sms.enums.SignTypeEnum;
import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.factory.AliYunSmsSynchronization;
import org.jiuling.aliyun.sms.factory.service.AliYunSignatureManagementService;
import org.jiuling.aliyun.sms.factory.service.verify.AliYunSignatureManagementVerify;
import org.jiuling.aliyun.sms.model.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 阿里云同步签名管理Service实现类
 * @author 紫灵
 * @date 2023年02月20日 13时59分46秒
 */
public class AliYunSyncSignatureManagementServiceImpl extends AliYunSmsSynchronization implements AliYunSignatureManagementService {


    public AliYunSyncSignatureManagementServiceImpl(AliYunSmsConfig aliYunSmsConfig) {
        super(aliYunSmsConfig);
    }

    @Override
    public AddSmsSignResponseModel addSmsSign(SignSourceEnum signSource, String remark, List<SignFileListDto> signFileList, SignTypeEnum signType, String signName) {
        AliYunSignatureManagementVerify.addSmsSignParam(signSource, remark, signFileList, signName);
        AddSmsSignRequest addSmsSignRequest = new AddSmsSignRequest();
        addSmsSignRequest.setSignName(signName);
        addSmsSignRequest.setSignSource(signSource.getCode());
        addSmsSignRequest.setRemark(remark);
        addSmsSignRequest.setSignType(signType.getCode());
        addSmsSignRequest.setSignFileList(signFileList.stream().map(signFileListDto -> {
            AddSmsSignRequest.AddSmsSignRequestSignFileList addSmsSignRequestSignFileList = new AddSmsSignRequest.AddSmsSignRequestSignFileList();
            addSmsSignRequestSignFileList.setFileContents(signFileListDto.getFileContents());
            addSmsSignRequestSignFileList.setFileSuffix(signFileListDto.getFileSuffix().name());
            return addSmsSignRequestSignFileList;
        }).collect(Collectors.toList()));
        try {
            return AddSmsSignResponseConvert.bodyToModel(create().addSmsSign(addSmsSignRequest).getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public DeleteSmsSignResponseModel deleteSmsSign(String signName) {
        AliYunSignatureManagementVerify.deleteSmsSignParam(signName);
        DeleteSmsSignRequest deleteSmsSignRequest = new DeleteSmsSignRequest();
        deleteSmsSignRequest.setSignName(signName);
        try {
            return DeleteSmsSignResponseConvert.bodyToModel(create().deleteSmsSign(deleteSmsSignRequest).getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public ModifySmsSignResponseModel modifySmsSign(String signName, SignSourceEnum signSource, String remark, List<SignFileListDto> signFileList, SignTypeEnum signType) {
        AliYunSignatureManagementVerify.modifySmsSignParam(signSource, remark, signFileList, signName);
        ModifySmsSignRequest modifySmsSignRequest = new ModifySmsSignRequest();
        modifySmsSignRequest.setSignName(signName);
        modifySmsSignRequest.setSignSource(signSource.getCode());
        modifySmsSignRequest.setRemark(remark);
        modifySmsSignRequest.setSignType(signType.getCode());
        modifySmsSignRequest.setSignFileList(signFileList.stream().map(signFileListDto -> {
            ModifySmsSignRequest.ModifySmsSignRequestSignFileList modifySmsSignRequestSignFileList = new ModifySmsSignRequest.ModifySmsSignRequestSignFileList();
            modifySmsSignRequestSignFileList.setFileContents(signFileListDto.getFileContents());
            modifySmsSignRequestSignFileList.setFileSuffix(signFileListDto.getFileSuffix().name());
            return modifySmsSignRequestSignFileList;
        }).collect(Collectors.toList()));
        try {
            return ModifySmsSignResponseConvert.bodyToModel(create().modifySmsSign(modifySmsSignRequest).getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public QuerySmsSignListResponseModel querySmsSignList(int pageNum, int pageSize) {
        QuerySmsSignListRequest querySmsSignListRequest = new QuerySmsSignListRequest();
        querySmsSignListRequest.setPageIndex(pageNum);
        querySmsSignListRequest.setPageSize(pageSize);
        try {
            return QuerySmsSignListResponseConvert.bodyToModel(create().querySmsSignList(querySmsSignListRequest).body);
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public QuerySmsSignResponseModel querySmsSign(String signName) {
        AliYunSignatureManagementVerify.querySmsSignParam(signName);
        QuerySmsSignRequest querySmsSignRequest = new QuerySmsSignRequest();
        querySmsSignRequest.setSignName(signName);
        try {
            return QuerySmsSignResponseConvert.bodyToModel(create().querySmsSign(querySmsSignRequest).getBody());
        } catch (Exception e) {
            e.getLocalizedMessage();
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }
}
